package learning.directory.Work.排序.Nf;

import java.util.Arrays;

/**
 * @author NIEF
 */

public class BubbleSortDay1 {

    public static void main(String[] args) {
        int[] arrSort = {10, 1, 35, 61, 89, 36, 55};
        //bubbleSortDay1(arrSort);
        bubbleSortOptimize(arrSort);
    }

    private static int[] bubbleSortDay1(int[] arrSort) {
        System.out.println("act=bubbleSortDay1 输入:" + Arrays.toString(arrSort));
        if (arrSort == null) {
            return null;
        }
        int tem;
        for (int i = 1; i < arrSort.length; i++) {
            for (int j = 0; j < arrSort.length - i; j++) {
                if (arrSort[j] > arrSort[j + 1]) {
                    tem = arrSort[j + 1];
                    arrSort[j + 1] = arrSort[j];
                    arrSort[j] = tem;
                }
            }
        }
        System.out.println("act=bubbleSortDay1 输出:" + Arrays.toString(arrSort));
        return arrSort;
    }

    public static int[] bubbleSortOptimize(int[] arrSort) {
        System.out.println("act=bubbleSortOptimize 输入:" + Arrays.toString(arrSort));
        boolean isExchange;
        int temp;
        for (int i = arrSort.length - 1; i > 0; i--) {
            isExchange = false;
            for (int j = 0; j < i; j++) {
                if (arrSort[j] > arrSort[j + 1]) {
                    temp = arrSort[j + 1];
                    arrSort[j + 1] = arrSort[j];
                    arrSort[j] = temp;
                    isExchange = true;
                }
            }
            if (!isExchange) {
                System.out.println(arrSort.length - i + "次后发现，没交换位置证明剩余已经是有序的，直接返回");
                break;
            }
        }
        System.out.println("act=bubbleSortOptimize 输出:" + Arrays.toString(arrSort));
        return arrSort;
    }

    //还是不会
}